// ActionScript file
package com.nstar.base.util
{
	/**
	 * 效果工具
	 * @author: 朱传明
	 */
   
    import com.nstar.base.effects.*;
    
    import flash.filters.BitmapFilter;
    import flash.filters.BitmapFilterQuality;
    import flash.filters.DropShadowFilter;
    import flash.filters.GlowFilter;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.utils.*;
    
    import mx.controls.Label;
    import mx.core.Application;
    import mx.core.IFlexDisplayObject;
    import mx.effects.Effect;
    import mx.effects.Fade;
    import mx.effects.Move;
    import mx.effects.Parallel;
    import mx.effects.Resize;
    import mx.effects.Sequence;
    import mx.effects.Zoom;
    import mx.effects.easing.Circular;
    import mx.events.EffectEvent;
    import mx.managers.PopUpManager;    
    
	public class EffectUtils
	{
		private static var timedProcess:uint = uint.MAX_VALUE;
		private static const X_OFFSET = 5;
		//在指定控件上高亮显示提示信息
		public static function showMessage(effectTarget:Object,effect:HighLightSequence,
			msgOwner:Label,msg:String, color:uint=0xfff7a0):void {
			effectTarget.visible = true;	
			effect.highColor = color;
			
			if(timedProcess != uint.MAX_VALUE) {
				clearTimeout(timedProcess);
			}
			
			msgOwner.visible = true;
			msgOwner.text = msg;
			timedProcess =setTimeout(clearMessage, 5000, msgOwner,effectTarget);
			effect.play([effectTarget]);
		}
		
		private static function clearMessage(message:Object,target:Object):void {
			message.visible = false;
		}	
		
        public static function getBitmapFilter(color:Number = 0xffffff,blur:Number=8):BitmapFilter {
            var alpha:Number = 0.8;
            var blurX:Number = blur;
            var blurY:Number = blur;
            var strength:Number = 2;
            var inner:Boolean = false;
            var knockout:Boolean = false;
            var quality:Number = BitmapFilterQuality.HIGH;
            return new GlowFilter(color,
                                  alpha,
                                  blurX,
                                  blurY,
                                  strength,
                                  quality,
                                  inner,
                                  knockout);
        }	
        
        //返回发光过滤器
        public static function setFlowEffect(object:Object,color:Number = 0xffffff,blur:Number=8):void{
        	var filter:BitmapFilter = getBitmapFilter(color,blur);
        	var myFilters:Array = new Array();
        	myFilters.push(filter);
        	object.filters = myFilters;
        }
	        

		
		//移动并消失
		public static function moveAndDisppear(target:Object,x:Number,y:Number,
			zoomWidthTo:Number=0,zoomHeightTo:Number=0):void{
			var app:* = Application.application;
			
			var className:String = getQualifiedClassName(target);
			var classRef:Class = getDefinitionByName(className) as Class;
			var tmp:Object = new classRef();
			//tmp.data = target.data;
			
			tmp.width = target.width;
			tmp.height = target.height;
			PopUpManager.addPopUp(tmp as IFlexDisplayObject,app);
			tmp.addEventListener(EffectEvent.EFFECT_END,endHandler);
			
			//获取target在屏幕上的位置
			var p:Point = target.localToGlobal(new Point());
			
			var par:Parallel = new Parallel(tmp);
			par.duration = 1000;
			var move:Move = new Move();
			move.xFrom = p.x;
			move.yFrom = p.y;
			move.xTo = x;
			move.yTo = y;
			
			var zoom:Zoom = new Zoom();
			zoom.zoomWidthTo = zoomWidthTo;
			zoom.zoomHeightTo = zoomHeightTo;
			
			var fade:Fade = new Fade();
			fade.alphaFrom = 1;
			fade.alphaTo = 0;
			
			par.addChild(move);
			par.addChild(zoom);
			par.addChild(fade);
			
			par.play();
			
		}	        			
		
		private static function endHandler(event:EffectEvent):void{
			PopUpManager.removePopUp(event.target as IFlexDisplayObject);
		}
		
		public static function moveDownEffect(target:Object):void{
			var app:* = Application.application;
			var move:Move = new Move(target);
			move.xFrom = app.width - target.width - X_OFFSET;
			move.yFrom = -target.height-10;
			move.xTo = app.width - target.width - X_OFFSET;
			move.yTo = 0;
			move.play();
		}
		
		public static function moveUpEffect(target:Object):void{
			var app:* = Application.application;
			var move:Move = new Move(target);
			target.addEventListener(EffectEvent.EFFECT_END,endHandler);
			move.xFrom = app.width - target.width - X_OFFSET;
			move.yFrom = 0;
			move.xTo = app.width - target.width - X_OFFSET;
			move.yTo = -target.height-10;
			move.play();
		}
		
		//设置容器的内部阴影效果,需要事先设置好容器的边框、背景色
		//典型的容器样式设置:
		//borderStyle="solid" borderColor="#565656" cornerRadius="5" backgroundColor="#565656"
		public static function shadow(inner:Boolean = true):Array{
			var ary:Array = new Array();
			//为了直观设置参数，参数用变量表示
			var distance:Number = 0;
			var angle:Number = 45;
			var color:uint = 0x1f2325;
			var alpha :Number = 0.5;
			var blurX:Number = 4;
			var blurY:Number = 4;
			var strength:Number = 2;
			var quality:int = BitmapFilterQuality.MEDIUM;
			ary.push(new DropShadowFilter(distance,angle,color,alpha,
				blurX,blurY,strength,quality,inner));
			return ary;
		}
		
		//将对象target从rectA处，移动并缩放到rectB处
		public static function moveResizeA2B(target:Object,
			rectA:Rectangle,rectB:Rectangle,callback:Function=null):Effect{
			var par:Parallel = new Parallel(target);
			//回调方法，在效果执行完毕后调用
			if(callback){
				par.addEventListener(EffectEvent.EFFECT_END,callback);
			}
						
			var m:Move = new Move();
			m.xFrom = rectA.x;
			m.yFrom = rectA.y;
			m.xTo = rectB.x;
			m.yTo = rectB.y;
			m.easingFunction = Circular.easeOut;
			
			var r:Resize = new Resize();
			r.widthFrom = rectA.width;
			r.heightFrom = rectA.height;
			r.widthTo = rectB.width;
			r.heightTo = rectB.height;
			
			
			var f:Fade = new Fade();
			f.alphaFrom = 0.5;
			f.alphaTo = 1;
			
			par.addChild(m);
			par.addChild(r);
			par.addChild(f);
			return par;
		}	
		
		//将对象target从rectA处，移动并缩放到rectB处，再移动并缩放到rectC处
		public static function moveResizeA2B2C(target:Object,
			rectA:Rectangle,rectB:Rectangle,rectC:Rectangle,callback:Function=null):Effect{
			var seque:Sequence = new Sequence(target);
			//回调方法，在效果执行完毕后调用
			if(callback){
				seque.addEventListener(EffectEvent.EFFECT_END,callback);
			}
			
			var e1:Effect = moveResizeA2B(target,rectA,rectB);
			var e2:Effect = moveResizeA2B(target,rectB,rectC);	
			seque.addChild(e1);
			seque.addChild(e2);
			return seque;
		}	
		
		//移动对象从p1 移到 p2
		public static function moveA2B(target:Object,p1:Point,p2:Point,
			callback:Function=null):Effect{
			var m:Move = new Move(target);
			//回调方法，在效果执行完毕后调用
			if(callback){
				m.addEventListener(EffectEvent.EFFECT_END,callback);
			}
			
			m.xFrom = p1.x;
			m.yFrom = p1.y;
			m.xTo = p2.x;
			m.yTo = p2.y;	
			return m;	
		}
		
		//移动对象从p1，移到p2，再移到p3
		public static function moveA2B2C(target:Object,p1:Point,p2:Point,p3:Point,
			callback:Function=null):Effect{
			var seqence:Sequence = new Sequence(target);
			//回调方法，在效果执行完毕后调用
			if(callback){
				seqence.addEventListener(EffectEvent.EFFECT_END,callback);
			}
			var m1:Move = new Move();
			m1.xFrom = p1.x;
			m1.yFrom = p1.y;
			m1.xTo = p2.x;
			m1.yTo = p2.y;
			
			var m2:Move = new Move();
			m2.xFrom = p2.x;
			m2.yFrom = p2.y;
			m2.xTo = p3.x;
			m2.yTo = p3.y;
			
			seqence.addChild(m1);
			seqence.addChild(m2);
			return seqence;
		}
	}
}